Hệ thống sổ địa chỉ đơn giản trong PHP

1 <?php
2 /*
3  * @author Shahrukh Khan
4  * @website http://www.thesoftwareguy.
in
5  * @facebbok https://www.facebook.com/Thesoftwareguy7
6  * @twitter https://twitter.com/thesoftwareguy7
7  * @googleplus https://plus.google.com/+thesoftwareguyIn
8  */

9 require_once
'./config.php';
10 include
'./header.php';
11 try
{
12    $sql =
"SELECT * FROM tbl_contacts WHERE 1 AND contact_id = :cid";
13    $stmt = $DB->prepare($sql);
14    $stmt->bindValue(
":cid", intval($_GET["cid"]));
15    
16    $stmt->execute();
17    $results = $stmt->fetchAll();
18 }
catch (Exception $ex) {
19   echo $ex->getMessage();
20 }
21 ?>
22
23 <div
class="row">
24   <ul
class="breadcrumb">
25       <li><a href=
"index.php">Home</a></li>
26       <li
class="active"><?php echo ($_GET["m"] == "update") ? "Edit" : "Add"; ?> Contacts</li>
27     </ul>
28 </div>
29
30   <div
class="row">
31     <div
class="panel panel-primary">
32       <div
class="panel-heading">
33         <h3
class="panel-title"><?php echo ($_GET["m"] == "update") ? "Edit" : "Add"; ?> New Contact</h3>
34       </div>
35       <div
class="panel-body">
36 <script
async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
37 <!-- responsivenoida -->
38 <ins
class="adsbygoogle"
39      style=
"display:block"
40      data-ad-client=
"ca-pub-5139634720777851"
41      data-ad-slot=
"2364206017"
42      data-ad-format=
"auto"
43      data-full-width-responsive=
"true"></ins>
44 <script>
45 (adsbygoogle = window.adsbygoogle || []).push({});
46 </script><br>
47
48         <form
class="form-horizontal" name="contact_form" id="contact_form" enctype="multipart/form-data" method="post" action="process_form.php">
49           <input type=
"hidden" name="mode" value="<?php echo ($_GET["m"] == "update") ? "update_old" : "add_new"; ?>" >
50           <input type=
"hidden" name="old_pic" value="<?php echo $results[0]["profile_pic"] ?>" >
51           <input type=
"hidden" name="cid" value="<?php echo intval($results[0]["contact_id"]); ?>" >
52           <input type=
"hidden" name="pagenum" value="<?php echo $_GET["pagenum"]; ?>" >
53           <fieldset>
54             <div
class="form-group">
55               <label
class="col-lg-4 control-label" for="first_name"><span class="required">*</span>First Name:</label>
56               <div
class="col-lg-5">
57                 <input type=
"text" value="<?php echo $results[0]["first_name"] ?>" placeholder="First Name" id="first_name" class="form-control" name="first_name"><span id="first_name_err" class="error"></span>
58               </div>
59             </div>
60             
61             <div
class="form-group">
62               <label
class="col-lg-4 control-label" for="middle_name">Middle Name:</label>
63               <div
class="col-lg-5">
64                 <input type=
"text" value="<?php echo $results[0]["middle_name"] ?>" placeholder="Middle Name" id="middle_name" class="form-control" name="middle_name">
65               </div>
66             </div>
67             
68             <div
class="form-group">
69               <label
class="col-lg-4 control-label" for="last_name"><span class="required">*</span>Last Name:</label>
70               <div
class="col-lg-5">
71                 <input type=
"text" value="<?php echo $results[0]["last_name"] ?>" placeholder="Last Name" id="last_name" class="form-control" name="last_name"><span id="last_name_err" class="error"></span>
72               </div>
73             </div>
74             
75             <div
class="form-group">
76               <label
class="col-lg-4 control-label" for="email_id"><span class="required">*</span>Email ID:</label>
77               <div
class="col-lg-5">
78                 <input type=
"text" value="<?php echo $results[0]["email_address"] ?>" placeholder="Email ID" id="email_id" class="form-control" name="email_id"><span id="email_id_err" class="error"></span>
79               </div>
80             </div>
81             
82             <div
class="form-group">
83               <label
class="col-lg-4 control-label" for="contact_no1"><span class="required">*</span>Contact No #1:</label>
84               <div
class="col-lg-5">
85                 <input type=
"text" value="<?php echo $results[0]["contact_no1"] ?>" placeholder="Contact Number" id="contact_no1" class="form-control" name="contact_no1"><span id="contact_no1_err" class="error"></span>
86                 <span
class="help-block">Maximum of 10 digits only and only numbers.</span>
87               </div>
88             </div>
89             
90             <div
class="form-group">
91               <label
class="col-lg-4 control-label" for="contact_no2">Contact No #2:</label>
92               <div
class="col-lg-5">
93                 <input type=
"text" value="<?php echo $results[0]["contact_no2"] ?>" placeholder="Contact Number" id="contact_no2" class="form-control" name="contact_no2"><span id="contact_no2_err" class="error"></span>
94                 <span
class="help-block">Maximum of 10 digits only and only numbers.</span>
95               </div>
96             </div>
97             
98             <div
class="form-group">
99               <label
class="col-lg-4 control-label" for="profile_pic">Profile picture:</label>
100               <div
class="col-lg-5">
101                 <input type=
"file" id="profile_pic" class="form-control file" name="profile_pic"><span id="profile_pic_err" class="error"></span>
102                 <span
class="help-block">Must me jpg, jpeg, png, gif, bmp image only.</span>
103               </div>
104             </div>
105             
106             <?php
if ($_GET["m"] == "update") { ?>
107             <div
class="form-group">
108               <div
class="col-lg-1 col-lg-offset-4">
109                 <?php $pic = ($results[
0]["profile_pic"] <> "" ) ? $results[0]["profile_pic"] : "no_avatar.png" ?>
110                 <a href=
"profile_pics/<?php echo $pic ?>" target="_blank"><img src="profile_pics/<?php echo $pic ?>" alt="" width="100" height="100" class="thumbnail" ></a>
111               </div>
112             </div>
113             <?php
114             }
115             ?>
116             
117             
118             
119             <div
class="form-group">
120               <label
class="col-lg-4 control-label" for="address">Address:</label>
121               <div
class="col-lg-5">
122                 <textarea id=
"address" name="address" rows="3" class="form-control"><?php echo $results[0]["address"] ?></textarea>
123               </div>
124             </div>
125             
126             <div
class="form-group">
127               <div
class="col-lg-5 col-lg-offset-4">
128                 <button
class="btn btn-primary" type="submit">Submit</button>
129               </div>
130             </div>
131           </fieldset>
132         </form>
133 <script
async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
134 <!-- responsivenoida -->
135 <ins
class="adsbygoogle"
136      style=
"display:block"
137      data-ad-client=
"ca-pub-5139634720777851"
138      data-ad-slot=
"2364206017"
139      data-ad-format=
"auto"
140      data-full-width-responsive=
"true"></ins>
141 <script>
142 (adsbygoogle = window.adsbygoogle || []).push({});
143 </script><br>
144
145       </div>
146     </div>
147   </div>
148
149 <script type=
"text/javascript">
150 $(document).ready(function() {
151     
152     
// the fade out effect on hover
153     $(
'.error').hover(function() {
154         $(
this).fadeOut(200);
155     });
156     
157     
158     $(
"#contact_form").submit(function() {
159         $(
'.error').fadeOut(200);
160         
if(!validateForm()) {
161             
// go to the top of form first
162             $(window).scrollTop($(
"#contact_form").offset().top);
163             
return false;
164         }
165         
return true;
166     });
167
168 });
169
170 function validateForm() {
171      
var errCnt = 0;
172      
173      
var first_name = $.trim( $("#first_name").val());
174      
var last_name = $.trim( $("#last_name").val());
175      
var email_id = $.trim( $("#email_id").val());
176      
var contact_no1 = $.trim( $("#contact_no1").val());
177      
var contact_no2 = $.trim( $("#contact_no2").val());
178      
179      
var profile_pic = $.trim( $("#profile_pic").val());
180
181     
// validate name
182     
if (first_name == "" ) {
183         $(
"#first_name_err").html("Enter your first name.");
184         $(
'#first_name_err').fadeIn("fast");
185         errCnt++;
186     }
else if (first_name.length <= 2 ) {
187         $(
"#first_name_err").html("Enter atleast 3 letter.");
188         $(
'#first_name_err').fadeIn("fast");
189         errCnt++;
190     }
191     
192     
if (last_name == "" ) {
193         $(
"#last_name_err").html("Enter your last name.");
194         $(
'#last_name_err').fadeIn("fast");
195         errCnt++;
196     }
else if (last_name.length <= 2 ) {
197         $(
"#last_name_err").html("Enter atleast 3 letter.");
198         $(
'#last_name_err').fadeIn("fast");
199         errCnt++;
200     }
201     
202     
if (!isValidEmail(email_id)) {
203         $(
"#email_id_err").html("Enter valid email.");
204         $(
'#email_id_err').fadeIn("fast");
205         errCnt++;
206     }
207     
208     
if (contact_no1 == "" ) {
209         $(
"#contact_no1_err").html("Enter first contact number.");
210         $(
'#contact_no1_err').fadeIn("fast");
211         errCnt++;
212     }
else if (contact_no1.length <= 9 || contact_no1.length > 10 ) {
213         $(
"#contact_no1_err").html("Enter 10 digits only.");
214         $(
'#contact_no1_err').fadeIn("fast");
215         errCnt++;
216     }
else if ( !$.isNumeric(contact_no1) ) {
217         $(
"#contact_no1_err").html("Must be digits only.");
218         $(
'#contact_no1_err').fadeIn("fast");
219         errCnt++;
220     }
221     
222     
if (contact_no2.length > 0) {
223       
if (contact_no2.length <= 9 || contact_no2.length > 10 ) {
224         $(
"#contact_no2_err").html("Enter 10 digits only.");
225         $(
'#contact_no2_err').fadeIn("fast");
226         errCnt++;
227     }
else if ( !$.isNumeric(contact_no2) ) {
228         $(
"#contact_no2_err").html("Must be digits only.");
229         $(
'#contact_no2_err').fadeIn("fast");
230         errCnt++;
231     }
232     }
233     
234     
235     
if (profile_pic.length > 0) {
236         
var exts = ['jpg','jpeg','png','gif', 'bmp'];
237         
var get_ext = profile_pic.split('.');
238         get_ext = get_ext.reverse();
239         
240        
241         
if ($.inArray ( get_ext[0].toLowerCase(), exts ) <= -1 ){
242           $(
"#profile_pic_err").html("Must me jpg, jpeg, png, gif, bmp image only..");
243           $(
'#profile_pic_err').fadeIn("fast");
244         }
245        
246     }
247     
248     
if(errCnt > 0) return false; else return true;
249 }
250
251 function isValidEmail(email) {
252   
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
253   
return regex.test(email);
254 }
255 </script>
256 <?php
257 include
'./footer.php';
258 ?>


Gõ tìm kiếm nhanh...